package com.software.frontend.mine.activity;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.software.frontend.MainActivity;
import com.software.frontend.R;
import com.software.frontend.fragments.Mine_MainFragment;
import com.software.frontend.loginAndRegist.entity.UserExtends;

public class SettingsActivity extends AppCompatActivity {

    private ImageView back;
    private TextView editInfomation, aboutUs, logOut;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        bindingViews();

        setListeners();


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1){
            System.out.println("sett1");
            Intent resultIntent = new Intent();
            resultIntent.putExtra("resultData", data.getSerializableExtra("resultData"));
            resultIntent.putExtra("head", data.getStringExtra("head"));
            setResult(Mine_MainFragment.RESULT_OK, resultIntent);
            finish();
        }
    }

    private void setListeners() {
        //自定义点击事件
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.button_back:
                        setResult(Mine_MainFragment.RESULT_CANCLE);
                        finish();
                        break;
                    case R.id.edit_infomation:
                        editInfomation();
                        break;
                    case R.id.about_us:
                        aboutUs();
                        break;
                    case R.id.log_out:
                        logOut();
                        break;
                }
            }
        };
        editInfomation.setOnClickListener(onClickListener);
        back.setOnClickListener(onClickListener);
        aboutUs.setOnClickListener(onClickListener);
        logOut.setOnClickListener(onClickListener);
    }

    private void bindingViews() {
        back = (ImageView) findViewById(R.id.button_back);
        editInfomation = (TextView) findViewById(R.id.edit_infomation);
        aboutUs = (TextView) findViewById(R.id.about_us);
        logOut = (TextView) findViewById(R.id.log_out);
    }

    private void logOut() {
        sp = this.getSharedPreferences("loginInformation", Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.remove("isLogin");
        editor.remove("userId");
        editor.putString("isLogin","F");
        editor.commit();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        setResult(3);
        finish();
    }

    private void aboutUs() {
        Intent intent = new Intent(this,AboutUsActivity.class);
        startActivity(intent);
    }

    private void editInfomation() {
        Intent intent = new Intent(this, EditPersonalInformationActivity.class);
        intent.putExtra("userExtends", (UserExtends) getIntent().getSerializableExtra("userExtends"));
        intent.putExtra("head", getIntent().getStringExtra("head"));
        startActivityForResult(intent, 0);
    }
}